package com.zpf.service.impl;

import com.zpf.dao.CartDao;
import com.zpf.dao.impl.CartDaoImpl;
import com.zpf.pojo.Cart;
import com.zpf.pojo.Goods;
import com.zpf.service.CartService;
import com.zpf.service.GoodsService;

import java.util.List;

public class CartServiceImpl implements CartService {

    CartDao cartDao = new CartDaoImpl();

    @Override
    public Cart findByUidAndGid(int uid, int pid) {
        return cartDao.findByUidAndGid(uid,pid);
    }

    @Override
    public void insert(Cart cart) {
        cartDao.insert(cart);
    }

    @Override
    public void updateCart(Cart cart) {
        cartDao.updateCart(cart);
    }

    @Override
    public List<Cart> findCartByUid(int uid) {
        //根据uid查询购物车
        List<Cart> carts = cartDao.findByUid(uid);

        //分别查询购物车列表中的每个商品信息
        if(carts != null && carts.size() > 0) {
            GoodsService goodsService = new GoodServiceImpl();
            for (Cart cart : carts) {

                Goods goods = goodsService.findById(cart.getPid());
                cart.setGoods(goods);
            }
        }
        return carts;
    }

    @Override
    public void deleteCart(int id, int pid) {
        cartDao.deleteCart(id,pid);
    }

    @Override
    public void clearCart(int uid) {
        cartDao.clearCart(uid);
    }
}
